package com.tmsps.auto.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ne.framework.data.util.ChkTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.tmsps.auto.action.BaseAction;

public class ActionInterceptor extends HandlerInterceptorAdapter {
	protected Logger log = LoggerFactory.getLogger(getClass());

	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse resp,
			Object handler) throws Exception {
		log.debug("开始处理分类注入");
		BaseAction action = (BaseAction) handler;
		// 设置分页参数
		String pageNum = req.getParameter("page.pageNum");
		int pm = ChkTools.getInteger(pageNum);
		pm = pm == 0 ? 1 : pm;
		action.page.setPageNum(pm);
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest req, HttpServletResponse resp,
			Object handler, ModelAndView mv) throws Exception {
		if (mv == null) {
			req.getSession().removeAttribute("tip");
			return;
		}
		if (handler instanceof BaseAction) {
			// 封装Action里的属性到request里
			BaseAction ac = (BaseAction) handler;
			if (ac.tip != null && ChkTools.isNotNull(ac.tip.getMsg())) {
				req.getSession().setAttribute("tip", ac.tip);
			}
			req.setAttribute("page", ac.page);
		} else {
			throw new Exception(handler.getClass() + "-->没有继承-->"
					+ BaseAction.class);
		}
	}

}
